$(function () {
    var dictionary_dataGrid = $("#dictionary_dataGrid");
    var dictionary_dialog = $("#dictionary_dialog");
    var dictionary_form = $("#dictionary_form");
    $("#dictionary_dataGrid").datagrid({
        url:'/systemDictionary/selectAll.do',
        fitColumns:true,
        toolbar:'#dictionary_toolbar',
        striped:true,
        rownumbers:true,
        singleSelect:true,
        fit:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'sn',title:'字典目录编码',width:80},
            {field:'name',title:'字典目录名称',width:80},
            {field:'intro',title:'字典目录简介',width:80}

        ]],
        onClickRow:function (index, row) {
            $("#dictionaryitem_dataGrid").datagrid({
                url:'/systemDictionaryItem/selectByDictionaryId.do?id='+row.id,
            })
        }
    })

    dictionary_dialog.dialog({
        width:300,
        height:250,
        closed:true,
        buttons:"#dictionary_buttons",
        onClose:function () {
            $("#dictionary_form").form('clear');
        }
    })
    var methodObj = {
        add: function () {
            dictionary_dialog.dialog('open');
            dictionary_dialog.dialog('setTitle', '新增字典目录');
        },

        edit: function () {
            var row = dictionary_dataGrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '至少选中一条数据!', 'warning');
                return;
            }
            dictionary_form.form('load', row);
            dictionary_dialog.dialog('open');
            dictionary_dialog.dialog('setTitle', '编辑字典目录');
        },
        save: function () {
            dictionary_form.form('submit', {
                url: '/systemDictionary/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示 ', '保存成功！', 'info', function () {
                            dictionary_dataGrid.datagrid('reload');
                            methodObj.cancel();
                        });
                    } else {
                        $.messager.alert('温馨提示 ', '保存失败！', 'error');
                    }
                }
            })
        },
        cancel: function () {
            dictionary_dialog.dialog('close');
        },
        remove :function () {
            var row = dictionary_dataGrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '至少选中一条数据!', 'warning');
                return;
            }

            $.messager.confirm('确定', "您确定删除吗?", function(r){
                if (r){
                    $.get("/systemDictionary/delete.do",{id:row.id},function (data) {
                        console.log(data);
                        if(data.success){
                            $.messager.alert('温馨提示','删除成功!','info');
                            dictionary_dataGrid.datagrid('reload')
                        } else {
                            $.messager.alert('温馨提示 ',data.msg,'error');
                        }
                    })
                }
            });
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
